<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title></title>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function() {
				$("#save").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
		        }).click(function(){
		        	if (validateEmpty()) {
		        		$("[name=balanceAmount]").removeAttr("disabled");
						disableField(["save","confirm"]);
						$("#save").addClass("ui-button-disabled ui-state-disabled");
						$("#confirm").addClass("ui-button-disabled ui-state-disabled");
		        		$("#job").val("save");
		        		$("#saveForm").submit();
		        	}
		        });
				$("#confirm").button({
		            icons: {
		                primary: "ui-icon-circle-check"
		            }
		        }).click(function(){
		        	if (validateEmpty()) {
						if(confirm('คุณต้องการยืนยันข้อมูล?')==true){
			        		$("[name=balanceAmount]").removeAttr("disabled");
							disableField(["save","confirm"]);
							$("#save").addClass("ui-button-disabled ui-state-disabled");
							$("#confirm").addClass("ui-button-disabled ui-state-disabled");
			        		$("#job").val("confirm");
			        		$("#saveForm").submit();
		        		}
		        	}
		        });
				$("#search").button({
		            icons: {
		                primary: "ui-icon-search"
		            }
		        }).click(function(){
		        	if (validateEmpty()) {
		        		$("#saveForm").submit();
		        	}
		        });
				$("#reset").button({
		            icons: {
		                primary: "ui-icon-cancel"
		            }
		        });
				
			/* 	$("[name=balanceAmount]").blur(function(){
					
				}); */
				$("#budgetYear").change(function(){
					disableField(["save","confirm"]);
					$("#save").addClass("ui-button-disabled ui-state-disabled");
					$("#confirm").addClass("ui-button-disabled ui-state-disabled");
				});
				$("#provinceId").change(function(){
					disableField(["save","confirm"]);
					$("#save").addClass("ui-button-disabled ui-state-disabled");
					$("#confirm").addClass("ui-button-disabled ui-state-disabled");
				});
				
				
			});
			function sumBalanceAmount(){
				var balanceAmount = document.getElementsByName("balanceAmount");
				var sum = 0 ;
				for (var i =0 ; i < balanceAmount.length ;i++){
					sum += toFloat(balanceAmount[i].value);
				}
				document.getElementById("sum").value = addCommasNoReplace2D(sum);
			}
		</script>
	</head>
	<body>
		<c:set var="auth" value="${el:getMapValue(menuAuth, 'ACC60104')}"/>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="saveForm" id="saveForm" action="${pageContext.request.contextPath}/acc6" method="post">
		<input type="hidden" name="app" id="app" value="ACC60104"/>
		<input type="hidden" name="job" id="job" value="${job}"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC60104}</b></th>
			</tr>
			<tr>
				<td width="20%" class="bold">จังหวัด : </td>
				<td>
					<c:if test="${userInfo.areaLevel=='3'}">
						<c:forEach var="provinceData" items="${userInfo.listUserProvice}">
							${provinceData.provinceName}
							<input type="hidden" name="provinceId" id="provinceId" value="${provinceData.provinceId}">
						</c:forEach>
					</c:if>
					<c:if test="${userInfo.areaLevel=='1' || userInfo.areaLevel=='2'}">
					<select name="provinceId" id="provinceId">
						<option value="">----- เลือก -----</option>
						<c:forEach var="provinceData" items="${userInfo.listUserProvice}">
							<c:if test="${provinceData.provinceId == provinceId}" var="provinceSeleted"/>
							<option value="${provinceData.provinceId}" ${provinceSeleted?'selected':''}>${provinceData.provinceName}</option>
						</c:forEach>
					</select>
					<span id="$provinceId" class="red">*</span>
					</c:if>
				</td>
			</tr>
			<tr>
				<td class="bold">ปีงบประมาณ : </td>
				<td>
					<select name="budgetYear" id="budgetYear">
						<option value="">----- เลือก -----</option>
						<c:forEach var="budgetYearData" items="${listBudgetYear}">
							<c:if test="${budgetYearData.BUDGET_YEAR <= 2556 }">
								<c:if  test="${budgetYear == budgetYearData.BUDGET_YEAR}" var="sel"/>
								<option value="${budgetYearData.BUDGET_YEAR}" ${sel?'selected':''}>${budgetYearData.BUDGET_YEAR}</option>
							</c:if>
						</c:forEach>
					</select><span id="$budgetYear" class="red">*</span>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
					<c:if test="${fn:contains(auth, 'S')}">
						<button id="search" type="button" class="acc67-button">ค้นหาข้อมูล</button>
					</c:if>
				</td>
			</tr>
		</table>
		<c:if test="${!empty listBudgetBalance}">
		<br>
			<c:set var="sum" value="0"/>
			<table class="acc67-grid ui-widget ui-widget-content" width="100%" cellpadding="1" cellspacing="1">
				<tr>
					<th class="ui-state-default">รหัส - ชื่อรหัสแจ้งจัดสรรเงินให้คู่สัญญา</th>
					<th class="ui-state-default">ยอดยกมา</th>
				</tr>
				<c:forEach var="data" items="${listBudgetBalance}" varStatus="x">
				<c:if test="${data.BALANCE_STATUS == 'Y' || !fn:contains(auth, 'U')}" var="isDiable"/>
				<tr class="grid-tr-${x.count % 2}">
					<td align="left">
						${data.ALLOCATE_CODE} - ${data.ALLOCATE_NAME}
						<input type="hidden" name="allocateCode" id="allocateCode" value="${data.ALLOCATE_CODE}">	
					</td>
					<td align="center">
						<input type="text" ${isDiable?'disabled':''} name="balanceAmount"  id="balanceAmount${x.count}" class="text-amount" value="<fmt:formatNumber pattern="#,##0.00" value="${data.BALANCE_AMOUNT}" />" onblur="this.value = addCommas2D(this.value); sumBalanceAmount();" onkeypress="return numberAndMinus(event);"/>
					</td>
				</tr>
				<c:set var="sum" value="${sum+data.BALANCE_AMOUNT}"/>
				</c:forEach>
				<tr>
					<td align="right">รวม</td>
					<td align="center"><input type="text" id="sum" readonly="readonly" class="text-amount" value="<fmt:formatNumber pattern="#,##0.00" value="${sum}" />"/></td>
				</tr>
					<c:if test="${!isDiable}">
						<tr>
							<td colspan="2" align="center">
								<c:if test="${fn:contains(auth, 'U')}">
									<button id="save" type="button" class="ac67-button">บันทึกข้อมูล</button>
								</c:if>
								<c:if test="${fn:contains(auth, 'U')}">
									<button id="confirm" type="button" class="ac67-button">ยืนยันข้อมูล</button>
								</c:if>
							</td>
						</tr>
					</c:if>
			</table>
			</fieldset>
			</c:if>
		</form>
	</body>
</html>